/* This file is part of Stimer. Stimer - Simple reminder tool.
    Copyright (C) 2012  Klimov Max <cleemmi@gmail.com>

    Stimer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    */

#include "trayview.h"
#include <QSystemTrayIcon>
#include <QEvent>
#include <QApplication>
#include "remindercontroller.h"
#include <QTimer>

TrayView::TrayView(ReminderController* controller, QObject *parent) :
    QSystemTrayIcon(parent),controller(controller)
{
    icons.push_back(QIcon(":/icons/tray/Bell-green-48.png"));
    icons.push_back(QIcon(":/icons/tray/Bell-yellow-48.png"));
    icons.push_back(QIcon(":/icons/tray/Bell-red-48.png"));
    setIcon(icons[0]);

    contextMenu = new QMenu("Stimer");
    astart = new QAction("Start",contextMenu);
    astop  = new QAction("Stop",contextMenu);
    aexit  = new QAction("Exit",contextMenu);

    ainfo.append(new QAction(contextMenu));
    ainfo.append(new QAction(contextMenu));
    ainfo.append(new QAction(contextMenu));
    for (QList<QAction*>::iterator it = ainfo.begin();it!=ainfo.end();++it) (*it)->setEnabled(false);

    connect(astart,SIGNAL(triggered()),this,SIGNAL(startClicked()));
    connect(astop,SIGNAL(triggered()),this,SIGNAL(stopClicked()));
    connect(aexit,SIGNAL(triggered()),this,SLOT(onExit()));

    connect(this,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconAction(QSystemTrayIcon::ActivationReason)));
    connect(this,SIGNAL(messageClicked()),this,SLOT(onMessageClicked()));

    alerter = new QTimer(this);
    alerter->setInterval(5000);
    alerter->setSingleShot(false);
    connect(alerter,SIGNAL(timeout()),this,SLOT(alert()));

    contextMenu->addAction(astart);
    contextMenu->addAction(astop);
    contextMenu->addSeparator();
    contextMenu->addActions(ainfo);
    contextMenu->addSeparator();
    contextMenu->addAction(aexit);

    this->setContextMenu(contextMenu);

    show();
}

void TrayView::setIconActive()
{
    setIcon(icons[1]);
    setVisible(true);
}

void TrayView::setIconRemind()
{
    setIcon(icons[2]);
    setVisible(true);
}

void TrayView::setIconDisabled()
{
    setIcon(icons[0]);
    setVisible(true);
}

void TrayView::start()
{
    setIconActive();
    astart->setEnabled(false);
    astop->setEnabled(true);
}

void TrayView::stop()
{
    setIconDisabled();
    astart->setEnabled(true);
    astop->setEnabled(false);
}


void TrayView::exit()
{
    QApplication::exit(0);
}

void TrayView::iconAction(QSystemTrayIcon::ActivationReason reason)
{
    if (alerter->isActive())
    {
        alerter->stop();
        emit stopClicked();
    }
    switch(reason)
    {
    case QSystemTrayIcon::DoubleClick:
        emit trayClicked();
        break;
    case QSystemTrayIcon::MiddleClick:
        emit startClicked();
        break;
    case QSystemTrayIcon::Context:
        this->getInfo();
    default: break;
    }
}

void TrayView::remind()
{
    now = now.currentTime();
    alert();
    alerter->start();
}

void TrayView::alert()
{
    showMessage("Alarm","Countdown ends in "+now.toString(),QSystemTrayIcon::Warning);
    setIconRemind();
}

void TrayView::getInfo()
{
    const QTime& time = controller->getTime();
    QTime eltime = controller->getElapsedTime();
    ainfo[0]->setText("Timer:       "+time.toString());
    ainfo[1]->setText("Elapsed:     "+eltime.toString());
    ainfo[2]->setText("Estimated: "+QTime(0,0,0,0).addMSecs(eltime.msecsTo(time)).toString());
}

void TrayView::onExit()
{
    controller->saveSettings();
    QApplication::exit(1);
}

void TrayView::onMessageClicked()
{
    iconAction(QSystemTrayIcon::Trigger);
}
